package com.yanqu.road.server.cmd.activity.shuihuyezicard;

import com.yanqu.road.entity.activity.shuihuyezipai.ShuiHuYeZiCardRoomData;
import com.yanqu.road.entity.activity.shuihuyezipai.config.ShuiHuYeZiCardRoomConfig;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.activity.ShuiHuYeZiCardProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.shuihuyezicard.ShuiHuYeZiCardModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.ShuiHuYeZiCardCommonMgr;
import com.yanqu.road.server.manger.activity.shuihuyezicard.ShuiHuYeZiCardMgr;
import com.yanqu.road.server.manger.activity.shuihuyezicard.ShuiHuYeZiCardPVERoom;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

/**
 * @Description
 * @Author cwq
 * @Data 2021/12/3 16:03
 */
@Cmd(code = GameProtocol.S_SHUIHUYEZICARD_START_GAME, desc = "水浒叶子牌 开始游戏")
public class ShuiHuYeZiCardStartGameCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ShuiHuYeZiCardProto.ShuiHuYeZiGetCardStartGameReqMsg reqMsg = ShuiHuYeZiCardProto.ShuiHuYeZiGetCardStartGameReqMsg.parseFrom(packet.getBytes());
        int activityId = reqMsg.getActivityId();
        int type = reqMsg.getType();
        int roomType = reqMsg.getRoomType();

        ret = ShuiHuYeZiCardMgr.canEntryActivity(player,activityId);
        if(ret != 0){
            return;
        }
        ShuiHuYeZiCardModule shuiHuYeZiCardModule = player.getModule(ShuiHuYeZiCardModule.class);
        if(type == 0){
            // 练习模式
            ShuiHuYeZiCardRoomData shuiHuYeZiCardRoomData = new ShuiHuYeZiCardRoomData();
            shuiHuYeZiCardRoomData.setActivityId(activityId);
            shuiHuYeZiCardRoomData.setRankScore(shuiHuYeZiCardModule.getUserData().getRankScore());
            shuiHuYeZiCardRoomData.setUserId(player.getUserId());
            shuiHuYeZiCardRoomData.setRoomId(Integer.MAX_VALUE);
            shuiHuYeZiCardRoomData.setNpcId(reqMsg.getNpcId());
            // 是否是引导房间
            boolean isGuide = roomType == -1;
            shuiHuYeZiCardModule.setExerciseRoom(new ShuiHuYeZiCardPVERoom(shuiHuYeZiCardRoomData,isGuide));
            shuiHuYeZiCardModule.getExerciseRoom().startGame();
            ShuiHuYeZiCardProto.ShuiHuYeZiGetCardStartGameRespMsg.Builder resp = ShuiHuYeZiCardProto.ShuiHuYeZiGetCardStartGameRespMsg.newBuilder();
            ShuiHuYeZiCardProto.ShuiHuYeZiCardRoomInfo.Builder roomInfo = shuiHuYeZiCardModule.getExerciseRoom().builderShuiHuYeZiCardRoomInfo(player.getUserId());
            resp.setRoomInfo(roomInfo);
            resp.setRet(0);
            player.sendPacket(ClientProtocol.U_SHUIHUYEZICARD_START_GAME,resp);
            return;
        }
        ShuiHuYeZiCardRoomConfig roomConfig = ShuiHuYeZiCardMgr.getConfig().getRoomConfigMap().get(roomType);
        if(roomConfig == null){
            ret = GameErrorCode.E_SHUIHUYEZICARD_ACTIVITY_ROOM_TYPE_NO_EXIST;
            return;
        }

        if(shuiHuYeZiCardModule.getUserData().getGetPropNum() < roomConfig.getTicketsCondition()){
            // 斗牌请帖不满足
            // 判断 是否在段位区间
            int level = ShuiHuYeZiCardCommonMgr.getLevel(ShuiHuYeZiCardMgr.getConfig(),shuiHuYeZiCardModule.getUserData().getRankScore());
            if(level < roomConfig.conditionList.get(0) || level > roomConfig.conditionList.get(1)){
                // 不在段位区间
                ret = GameErrorCode.E_SHUIHUYEZICARD_ACTIVITY_RANK_SCORE_INCOMPATIBLE;
                return;
            }
        }

        shuiHuYeZiCardModule.refreshUser();
        int times = shuiHuYeZiCardModule.getUserData().getJoinRoomTimesMap().getOrDefault(roomType,0);
        if(type == 1){
            // pve模式
            if(shuiHuYeZiCardModule.getNpcRoom() != null && shuiHuYeZiCardModule.getNpcRoom().getRoomInfo().getRoomId() != 0){
                // 已开
                ret = GameErrorCode.E_SHUIHUYEZICARD_ACTIVITY_IN_GAME;
                // 加个容错
                shuiHuYeZiCardModule.synUserData();
                return;
            }
            if(times >= roomConfig.getFreePlay()){
                // 扣道具
                Property consume = PropertyHelper.parseStringToProperty(roomConfig.getConsume());
                if(!player.getModule(CurrencyModule.class).removeCurrency(consume, eLogMoneyType.ShuiHuYeZiCardActivity,eLogMoneyType.ShuiHuYeZiCardActivityPveConsume)){
                    ret = GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
                    return;
                }
            }
            if(shuiHuYeZiCardModule.getNpcRoom() == null){
                ShuiHuYeZiCardRoomData shuiHuYeZiCardRoomData = new ShuiHuYeZiCardRoomData();
                shuiHuYeZiCardRoomData.setActivityId(activityId);
                shuiHuYeZiCardRoomData.setUserId(player.getUserId());
                shuiHuYeZiCardRoomData.setInsertOption();
                shuiHuYeZiCardModule.setNpcRoom(new ShuiHuYeZiCardPVERoom(shuiHuYeZiCardRoomData));
            }
            shuiHuYeZiCardModule.getNpcRoom().getRoomInfo().setNpcId(reqMsg.getNpcId());
            shuiHuYeZiCardModule.getNpcRoom().getRoomInfo().setRoomId(roomType);
            shuiHuYeZiCardModule.getNpcRoom().getRoomInfo().setRankScore(shuiHuYeZiCardModule.getUserData().getRankScore());
            shuiHuYeZiCardModule.getNpcRoom().startGame();

            shuiHuYeZiCardModule.addJoinRoomTimes(roomType,1);

            ShuiHuYeZiCardProto.ShuiHuYeZiGetCardStartGameRespMsg.Builder resp = ShuiHuYeZiCardProto.ShuiHuYeZiGetCardStartGameRespMsg.newBuilder();
            ShuiHuYeZiCardProto.ShuiHuYeZiCardRoomInfo.Builder roomInfo = shuiHuYeZiCardModule.getNpcRoom().builderShuiHuYeZiCardRoomInfo(packet.getPlayerId());
            resp.setRoomInfo(roomInfo);
            resp.setRet(0);
            player.sendPacket(ClientProtocol.U_SHUIHUYEZICARD_START_GAME,resp);
        }else {
            // pvp 模式
            // 判断是否在匹配 期间
            if(!ShuiHuYeZiCardCommonMgr.inMatchTime(ShuiHuYeZiCardMgr.getConfig())){
                ret = GameErrorCode.E_SHUIHUYEZICARD_ACTIVITY_NO_IN_MATCH_TIME;
                return;
            }
            if(times >= roomConfig.getFreePlay()){
                // 判断道具是否充足
                Property consume = PropertyHelper.parseStringToProperty(roomConfig.getConsume());
                if(!player.getModule(CurrencyModule.class).currencyIsEnough(consume)){
                    ret = GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
                    return;
                }
                // 预扣除
                player.getModule(PreReduceModule.class).preReduce(roomConfig.getConsume());
            }

            ShuiHuYeZiCardProto.ShuiHuYeZiGetCardStartGameToCrossMsg.Builder matchInfo = ShuiHuYeZiCardProto.ShuiHuYeZiGetCardStartGameToCrossMsg.newBuilder();
            matchInfo.setActivityId(ShuiHuYeZiCardMgr.getActivityInfo().getActivityId());
            matchInfo.setUserId(player.getUserId());
            matchInfo.setRankScore(shuiHuYeZiCardModule.getUserData().getRankScore());
            matchInfo.setRoomType(roomType);
            matchInfo.setPlayerInfo(PlayerBasePb.parsePlayerBaseTempMsg(UserMgr.getUserBaseInfo(player.getUserId(), GameServer.getInstance().getServerId())));
            matchInfo.addAllCardList(shuiHuYeZiCardModule.getShuiHuYeZiCardUserCardEntitytList());
            player.sendPacket(CrossProtocol.C_CROSS_SHUIHUYEZICARD_START_GAME,matchInfo);
        }
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            ShuiHuYeZiCardProto.ShuiHuYeZiGetCardStartGameRespMsg.Builder resp = ShuiHuYeZiCardProto.ShuiHuYeZiGetCardStartGameRespMsg.newBuilder();
            resp.setRet(ret);
            player.sendPacket(ClientProtocol.U_SHUIHUYEZICARD_START_GAME,resp);
        }
    }
}
